package com.cmcm.login.xiaomi;

import android.app.Activity;
import android.os.AsyncTask;
import android.support.v4.os.OperationCanceledException;
import android.util.Log;
import android.widget.Toast;

import com.xiaomi.account.openauth.XMAuthericationException;
import com.xiaomi.account.openauth.XiaomiOAuthConstants;
import com.xiaomi.account.openauth.XiaomiOAuthFuture;
import com.xiaomi.account.openauth.XiaomiOAuthResults;
import com.xiaomi.account.openauth.XiaomiOAuthorize;

import java.io.IOException;

import cmcm.com.mutildepend.BuildConfig;

/**
 * Created by su  on 2016/7/21.
 * com.cmcm.login.xiaomi
 * des:
 * email:suyanjiao@conew.com
 * 1553202383
 * 依据 https://github.com/xiaomipassport/oauth-Android-sdk 开发文档开发
 */
public class LoginWithXiaoMi {
    private static long appId = 2882303761517492641l;
    private final static String redirctUrl = "https://iptlogin.ksmobile.com/third/login";

    public static void preparedLogin(final Activity activity, final int from) {

        new AsyncTask<Void, Void, XiaomiOAuthResults>() {
            @Override
            protected XiaomiOAuthResults doInBackground(Void... params) {
                // 这一句可以在UI线程调用
                XiaomiOAuthFuture<XiaomiOAuthResults> future = new XiaomiOAuthorize()
                        .setAppId(appId)
                        .setRedirectUrl(redirctUrl)
                        .setScope(new int[]{XiaomiOAuthConstants.SCOPE_PROFILE, XiaomiOAuthConstants.SCOPE_OPEN_ID})
                        .startGetAccessToken(activity);
                try {
                    XiaomiOAuthResults results = future.getResult();
                    return results;
                } catch (IOException e1) {
                    // error
                } catch (OperationCanceledException e1) {
                    // user cancel
                } catch (XMAuthericationException e1) {
                    // error
                } catch (android.accounts.OperationCanceledException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(XiaomiOAuthResults results) {
                super.onPostExecute(results);
                if (results == null) {
                    return;
                }
                if (results.hasError()) {
                    int errorCode = results.getErrorCode();
                    String errorMessage = results.getErrorMessage();
                    if (BuildConfig.DEBUG) {
                        Log.e("Login", "Error code " + errorCode + "Error msg" + errorMessage);
                    }


                } else {
                    String accessToken = results.getAccessToken();
                    final String macKey = results.getMacKey();
                    String fakeToken = accessToken+"|"+macKey;
                    Log.e("suj",fakeToken);
//                    LoginService.start_ACTION_LOGIN_BY_XIAO(activity, fakeToken, null, from);
                }
            }
        }.execute();

    }

}
